<?php

namespace XXXX\PlatformBundle\Util;
use XXXX\PlatformBundle\Entity\Base\Course;
use XXXX\PlatformBundle\Entity\Content\Activity;

/**
 * Description of Assignment
 * How many fields of date should I use on new.html.twig
 *
 * @author Ryszard Ratajczak <rratajczak@xxxx.com.pl>
 */
class AssignmentOptions
{
    /**
     * EntityManager
     *
     * @var EntityManager $em - entity manager
     */
    private $em;

    /**
     * Set EntityManager
     *
     * @param EntityManager $em - entity manager
     */
    public function __construct($em)
    {
        $this->em = $em;
    }

    /**
     * @param \DateTime $courseExpirationDate
     *
     * @return array    $dateFields
     */
    public function createDueDateOption (\DateTime $courseExpirationDate)
    {
        $dateFields = array(
            'cd' => 0,
            'nm' => 0,
            'ow' => 0,
            'tw' => 0,
        );

        $now = new \DateTime();
        if ($courseExpirationDate > $now) {
            $dateFields['cd'] = 1;
        }

        $testDate = new \DateTime('next monday');
        if ($courseExpirationDate > $testDate) {
            $dateFields['nm'] = 1;
        }

        if ($courseExpirationDate > $now->add(new \DateInterval('P1W'))) {
            $dateFields['ow'] = 1;
        }

        if ($courseExpirationDate > $now->add(new \DateInterval('P2W'))) {
            $dateFields['tw'] = 1;
        }

        return $dateFields;
    }

    /**
     * Prepare student id to be blocked when creating assignments
     *
     * @param Course   $course   Course
     * @param Activity $activity Activity
     *
     * @return array $assignedStudentsToBlock student ids to block in form
     */
    public function assignedStudentsToBlock (Course $course, Activity $activity)
    {
        $assignedStudentsToBlock = array();

        if ($course->getExpirationDate() >= new \DateTime()) {
            $userAssignments = $this->em
                ->getRepository('XXXXPlatformBundle:Base\UserAssignment')
                ->getNotPracticesUsersAssignments($activity->getLatestContent(), $course);
            foreach ($userAssignments as $userAssignment) {
                $assignedStudentsToBlock[] = $userAssignment->getUser()->getId();
            }
        }

        return $assignedStudentsToBlock;
    }
}
